AnnieRoot.index=AnnieRoot.index||{};
index.MakeMc = function () {
    var s = this;
    annie.Sprite.call(s);
    /*_a2x_need_start*/s.bg_mc=null;s.c0_mc=null;s.c1_mc=null;s.c2_mc=null;s.c3_mc=null;s.car_mc=null;s.close_btn=null;s.i0_mc=null;s.i1_mc=null;s.i2_mc=null;s.i3_mc=null;s.icons_mc=null;s.k_mc=null;s.ok_btn=null;s.s1_mc=null;s.s2_mc=null;s.yun_mc=null;/*_a2x_need_end*/

    annie.initRes(s,"index","MakeMc");
    s.c0_mc.gotoAndPlay(2);
    s.ok_btn.initButton();
    //s.ok_btn.mouseEnable = false;
    s.ok_btn.mouseChildren = false;
    //s.ok_btn.alpha = 0.5;
    s.car_mc.gotoAndStop(1);
    s.car_mc.mouseEnable = false;
    s.k_mc.mouseEnable = false;
    s.close_btn.mouseChildren = false;
    //s.icons_mc.visible = false;
    //s.icons_mc.mouseChildren = false;
    s.yun_mc.mouseEnable = false;
    s.bg_mc.mouseChildren = false;
    s.k_mc.down_mc.info_mc.visible = false;
    s.k_mc.down_mc.saveTip_mc.visible = false;
    var mask = s.bg_mc.mask;
    mask.visible = false;
    s.bg_mc.mask = null;
    var isFirst = -1;
    var curDragIcon = null;
    var id = 0;
    for (var i = 0; i < 4; i++) {
        s["i" + i + "_mc"].mouseChildren = false;
        s["c" + i + "_mc"].addEventListener(annie.Event.END_FRAME, function (e) {
            if (e.data.frameIndex == 1) {
                if (e.target.name == "c0_mc") {
                    s.car_mc.getChildAt(0).play();
                } else if (e.target.name == "c1_mc") {
                    s.bg_mc.play(false);
                } else if (e.target.name == "c2_mc") {
                    // s.icons_mc.visible = true;
                    // for (var i = 0; i < s.icons_mc.children.length; i++) {
                    // if (i != index.MakeMc.iconId) {
                    //     s.icons_mc.getChildAt(i).visible = false;
                    // } else {
                    //     s.icons_mc.getChildAt(i).visible = true;
                    // }
                    var icon = new index["IconItem" + index.MakeMc.iconId]();
                    icon.mouseChildren = false;
                    icon.isIcon = true;
                    s.addChildAt(icon, s.children.length - 9);
                    icon.x = 320;
                    icon.y = 400;
                    icon.scaleY = icon.scaleX = 1.5;
                    //s.ok_btn.mouseEnable = true;
                    //s.ok_btn.alpha = 1;
                    s.close_btn.x = -1000;
                    // }
                    drawLine(icon);
                    curDragIcon = icon;
                    isFirst++;
                    if (isFirst == 0) {
                        s.mouseEnable = false;
                        s.s1_mc.point_mc.gotoAndPlay(2);
                        s.s2_mc.point_mc.gotoAndPlay(2);
                        annie.Tween.to(icon, 0.2, {
                            delay: 0.2, scaleX: 2, scaleY: 2, yoyo: 2, onUpdate: function () {
                                drawLine(icon);
                            }, onComplete: function () {
                                isFirst++;
                                if (isFirst == 4) {
                                    annie.Tween.to(icon, 0.2, {
                                        delay: 0.2, rotation: 30, yoyo: 2, onUpdate: function () {
                                            drawLine(icon);
                                        }, onComplete: function () {
                                            isFirst++;
                                            if (isFirst == 8) {
                                                s.mouseEnable = true;
                                                s.stage.isMultiTouch = true;
                                                s.s1_mc.point_mc.visible = false;
                                                s.s2_mc.point_mc.visible = false;
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    }
                } else if (e.target.name == "c3_mc") {
                    //让车的轮胎更换
                    annie.globalDispatcher.dispatchEvent("onChangeWheel");
                }
                id = -1;
            }
        })
    }
    s.addEventListener(annie.MouseEvent.CLICK, function (e) {
        if (e.target.name == "i0_mc") {
            if (id != 0) {
                if (id != -1)
                    s["c" + id + "_mc"].gotoAndStop(1);
                id = 0;
                s["c" + id + "_mc"].gotoAndPlay(1, true);
            }
        } else if (e.target.name == "i1_mc") {
            if (id != 1) {
                if (id != -1)
                    s["c" + id + "_mc"].gotoAndStop(1);
                id = 1;
                s["c" + id + "_mc"].gotoAndPlay(1, true);
            }
        } else if (e.target.name == "i2_mc") {
            if (id != 2) {
                if (id != -1)
                    s["c" + id + "_mc"].gotoAndStop(1);
                id = 2;
                s["c" + id + "_mc"].gotoAndPlay(1, true);
            }
        } else if (e.target.name == "i3_mc") {
            if (id != 3) {
                if (id != -1)
                    s["c" + id + "_mc"].gotoAndStop(1);
                id = 3;
                s["c" + id + "_mc"].gotoAndPlay(1, true);
            }
        } else if (e.target.name == "ok_btn") {
            //生成结果页
            for (var i = 0; i < 4; i++) {
                s["i" + i + "_mc"].visible = false;
                s["c" + i + "_mc"].visible = false;
            }
            s.ok_btn.visible = false;
            s.k_mc.down_mc.info_mc.visible = true;
            shape.visible = false;
            s.close_btn.x = -1000;
            s.s1_mc.x = -1000;
            s.s2_mc.x = -1000;
            s.yun_mc.gotoAndStop(150);
            var rect = new annie.Rectangle();
            rect.x = s.stage.viewRect.x + 5;
            rect.y = s.stage.viewRect.y + 5;
            rect.width = s.stage.viewRect.width - 5;
            rect.height = s.stage.viewRect.height - 5;
            s.bg_mc.mask = mask;
            index.MakeMc.picture = annie.toDisplayDataURL(s, rect, null, index.MakeMc.color[index.MakeMc.sceneId]);
            s.k_mc.down_mc.y -= 40;
            s.k_mc.down_mc.saveTip_mc.visible = true;
            index.MakeMc.picture1 = annie.toDisplayDataURL(s, rect, null, index.MakeMc.color[index.MakeMc.sceneId]);
            annie.globalDispatcher.dispatchEvent("onLoaded", 2);
        } else if (e.target.name == "close_btn") {
            s.removeChild(curDragIcon);
            curDragIcon.destroy();
            curDragIcon = null;
            shape.visible = false;
            s.close_btn.x = -1000;
            s.s1_mc.x = -1000;
            s.s2_mc.x = -1000;
        }
    });
    var rect = new annie.Rectangle(150, 50, 400, 650);
    s.addEventListener(annie.MouseEvent.MOUSE_DOWN, function (e) {
        if (e.target.isIcon) {
            curDragIcon = e.target;
            e.target.startDrag(false, rect);
        }
        if (e.target.name != "close_btn") {
            shape.visible = false;
            s.close_btn.x = -1000;
            s.s1_mc.x = -1000;
            s.s2_mc.x = -1000;
        }

    });
    var shape = new annie.Shape();
    var color = "#666666";
    shape.mouseEnable = false;
    s.addChildAt(shape, s.children.length - 8);
    var radio = 4;
    var drawLine = function (target) {
        s.addChildAt(target, s.children.length - 9);
        target.getDrawRect();
        var rect =annie.DisplayObject._transformRect;
        rect.y -= 5;
        rect.x -= 5;
        rect.width += 5;
        rect.height += 5;
        shape.clear();
        shape.addDraw("setLineDash", [[radio, radio], 0]);
        shape.beginStroke(color, radio);
        shape.drawRect(rect.x, rect.y, rect.width, rect.height);
        shape.endStroke();

        // shape.beginFill(color);
        // shape.drawCircle(rect.x, rect.y, radio);
        // shape.endFill();

        /*shape.beginFill(color);
        shape.drawCircle(rect.x + rect.width, rect.y, radio + 2);
        shape.endFill();*/

        shape.beginFill(color);
        shape.drawCircle(rect.x + rect.width, rect.y + rect.height, radio + 2);
        shape.endFill();

        /*shape.beginFill(color);
        shape.drawCircle(rect.x, rect.y + rect.height, radio + 2);
        shape.endFill();*/

        shape.beginFill(color);
        shape.drawCircle(rect.x + rect.width * 0.5, rect.y, radio);
        shape.endFill();

        shape.beginFill(color);
        shape.drawCircle(rect.x, rect.y + rect.height * 0.5, radio);
        shape.endFill();

        shape.beginFill(color);
        shape.drawCircle(rect.x + rect.width, rect.y + rect.height * 0.5, radio);
        shape.endFill();

        shape.beginFill(color);
        shape.drawCircle(rect.x + rect.width * 0.5, rect.y + rect.height, 5);
        shape.endFill();
        shape.visible = true;
        s.close_btn.x = rect.x;
        s.close_btn.y = rect.y;
        s.s1_mc.x = rect.x + rect.width;
        s.s1_mc.y = rect.y;
        s.s2_mc.x = rect.x;
        s.s2_mc.y = rect.y + rect.height;
    };
    s.addEventListener(annie.MouseEvent.MOUSE_UP, function (e) {
        if (e.target.isIcon) {
            drawLine(e.target);
        }
        if (curDragIcon) {
            curDragIcon.stopDrag()
        }
    });
    s.addEventListener(annie.Event.ADD_TO_STAGE, function (e) {
        s.yun_mc.y = s.yun_mc.y / s.stage.desHeight * s.stage.viewRect.height + s.stage.viewRect.y;
        var newY = s.stage.desHeight - s.stage.viewRect.y - (s.stage.desHeight - s.i0_mc.y - 91) / s.stage.desHeight * s.stage.viewRect.height - 91;
        for (var i = 0; i < 4; i++) {
            s["i" + i + "_mc"].y = newY;
            s["c" + i + "_mc"].y = newY;
        }
        s.ok_btn.y = newY;
        s.stage.addEventListener(annie.TouchEvent.ON_MULTI_TOUCH, function (e) {
            if (curDragIcon) {
                if (e.rotate == 0 && e.scale == 0) {
                    drawLine(curDragIcon);
                } else {
                    shape.visible = false;
                    s.close_btn.x = -1000;
                    s.s1_mc.x = -1000;
                    s.s2_mc.x = -1000;
                    e.target.stopDrag();
                    curDragIcon.scaleX += e.scale;
                    curDragIcon.scaleY += e.scale;
                    curDragIcon.rotation += e.rotate;
                    if (curDragIcon.scaleX < 0.2) {
                        curDragIcon.scaleX = curDragIcon.scaleY = 0.5;
                    } else if (curDragIcon.scaleX > 4.0) {
                        curDragIcon.scaleX = curDragIcon.scaleY = 4.0;
                    }
                }
            }
        });
    })
};
A2xExtend(index.MakeMc, annie.Sprite);
index.MakeMc.sceneId = 0;
index.MakeMc.iconId = 0;
index.MakeMc.wheelId = 0;
index.MakeMc.carId = 0;
index.MakeMc.color = ["#E25A5D", "#ffd110", "#8cd392", "#78d8f9", "#b4d350"];

